package com.xunk.equipment.service.ruying;

import com.google.common.collect.Lists;
import com.xunk.common.core.domain.EquipmentResponse;
import com.xunk.common.redis.service.RedisService;
import com.xunk.equipment.domain.EquipmentDO;
import com.xunk.equipment.domain.EquipmentPropertiesData;
import com.xunk.equipment.service.BaseService;
import com.xunk.equipment.service.IThreeKeySwitchService;
import com.xunk.equipment.vo.response.SwitchKey;
import com.xunk.equipment.vo.response.ThreeKeySwitchInfoResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Map;
import java.util.Objects;

/**
 * @ProjectName: xunk
 * @Package: com.xunk.equipment.service.ruying
 * @ClassName: TwoKeySwitchService
 * @Author: yangwendong
 * @Description:
 * @Date: 2021/3/31 18:59
 * @Version: 1.0
 */
@Slf4j
@Service(value = "ruying-three-key-switch")
public class ThreeKeySwitchService implements IThreeKeySwitchService {

    @Autowired
    RedisService redisService;

    @Autowired
    BaseService baseService;

    @Autowired
    RuYingControlService ruYingControlService;

    /**
     * 开关 开和关的控制
     */
    private final static String left = "{0}.10.11";
    private final static String middle = "{0}.13.14";
    private final static String right = "{0}.16.17";

    private static final String MODEL_ID = ".1.5";
    private static final String SERIAL_NUMBER_ID = ".1.7";
    private static final String FIRMWARE_REVISION_ID = ".1.2";

    @Override
    public EquipmentResponse getEquipmentInfo(EquipmentDO equipmentDO) {

        String deviceSn = equipmentDO.getDeviceSn();
        Long deptId = equipmentDO.getDeptId();
        Map<String, String> cacheMap = ruYingControlService.getShadowByDeviceSn(deviceSn, deptId);
        // 单键开关的开关状态，pid = did.10.11 ,value值为true或false
        String leftOnStateValue = getOnStateValue(MessageFormat.format(left, deviceSn), cacheMap);

        String middleOnStateValue = getOnStateValue(MessageFormat.format(middle, deviceSn), cacheMap);

        String rightOnStateValue = getOnStateValue(MessageFormat.format(right, deviceSn), cacheMap);

        ThreeKeySwitchInfoResponse response = new ThreeKeySwitchInfoResponse();
        response.setDeviceSn(deviceSn);
        response.setLeft(new SwitchKey(Boolean.valueOf(leftOnStateValue)));
        response.setMiddle(new SwitchKey(Boolean.valueOf(middleOnStateValue)));
        response.setRight(new SwitchKey(Boolean.valueOf(rightOnStateValue)));

        response.setModel(cacheMap.get(deviceSn.concat(MODEL_ID)));
        response.setFirmWare(cacheMap.get(deviceSn.concat(FIRMWARE_REVISION_ID)));
        response.setSerialNumber(cacheMap.get(deviceSn.concat(SERIAL_NUMBER_ID)));
        response.setSupplier(equipmentDO.getSupplier());
        response.setCandidateModel(baseService.getCandidateModel(equipmentDO.getDeviceBaseId()));
        return response;
    }

    private String getOnStateValue(String pid, Map<String, String> cacheMap) {
        return cacheMap.getOrDefault(pid, "false");
    }

    @Override
    public Boolean control(EquipmentDO equipmentDO, String type, String value) {
        // 查询当前设备对应的ownerId
        String deviceSn = equipmentDO.getDeviceSn();
        Long deptId = equipmentDO.getDeptId();
        String ownId = baseService.findOwnIdByDeptId(deptId);
        switch (type) {
            // 控制开关
            case "leftOn":
                return controlOn(MessageFormat.format(left, deviceSn), ownId, Boolean.valueOf(value));
            case "middleOn":
                return controlOn(MessageFormat.format(middle, deviceSn), ownId, Boolean.valueOf(value));
            case "rightOn":
                return controlOn(MessageFormat.format(right, deviceSn), ownId, Boolean.valueOf(value));
            default:
                return null;
        }
    }

    private Boolean controlOn(String pid, String ownerId, Object value) {
        EquipmentPropertiesData data = new EquipmentPropertiesData();
        EquipmentPropertiesData.PropertyData propertyData = new EquipmentPropertiesData.PropertyData();
        propertyData.setPid(pid);
        propertyData.setValue(value);
        ArrayList<EquipmentPropertiesData.PropertyData> objects = Lists.newArrayList(propertyData);
        data.setProperties(objects);
        String s = ruYingControlService.setProperties(ownerId, data);
        return Objects.equals(s, "success");
    }
}
